package com.isunland.managesystem.ui;

import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import butterknife.BindView;
import com.github.clans.fab.FloatingActionButton;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BasePagerActivity;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.widget.RevealBackgroundView;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/5/12 10:16
 */
public class UserGuideImageFragment extends BaseFragment
    implements RevealBackgroundView.OnStateChangeListener {
  @BindView(R.id.vRevealBackground) RevealBackgroundView vRevealBa1ckground;

  @Override protected int initLayoutId() {
    return R.layout.fragment_user_guide_image;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    ImageView ivGuide = (ImageView) view.findViewById(R.id.iv_image_header);
    ivGuide.setBackgroundResource(
        MyUtils.isWeiJia() ? R.drawable.wj_user_guide : R.drawable.user_guide);
    ivGuide.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        startGuideImage();
      }
    });
    //noinspection deprecation
    vRevealBa1ckground.setFillPaintColor(getResources().getColor(R.color.primary_pink), 101);
    //vRevealBa1ckground.setFillPaintColor(0x88e91e63);
    vRevealBa1ckground.setOnStateChangeListener(this);
    final FloatingActionButton fabPlay = (FloatingActionButton) view.findViewById(R.id.fab_play);
    fabPlay.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        vRevealBa1ckground.setVisibility(View.VISIBLE);
        vRevealBa1ckground.startFromLocation(getViewLocation(v));
      }
    });
  }

  private int[] getViewLocation(View v) {
    int[] startingLocation = new int[2];
    v.getLocationOnScreen(startingLocation);
    startingLocation[0] += v.getWidth() / 2;
    int statusBar = getStatusBar();

    startingLocation[1] =
        startingLocation[1] + v.getHeight() / 2 - ((BasePagerActivity) mActivity).getTabLayout()
            .getHeight() - ((BasePagerActivity) mActivity).getAppBar().getHeight() - statusBar;

    return startingLocation;
  }

  private int getStatusBar() {
    //屏幕
    DisplayMetrics dm = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    //应用区域
    Rect outRect1 = new Rect();
    mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
    return dm.heightPixels - outRect1.height();
  }

  private void startGuideImage() {
    GuideImageActivity.newInstance(UserGuideImageFragment.this, GuideImageActivity.class, null, 0);
  }

  @Override public void onStateChange(int state) {
    LogUtil.i("state=" + state);
    if (RevealBackgroundView.STATE_FINISHED == state) {
      startGuideImage();
      mActivity.overridePendingTransition(0, 0);
      vRevealBa1ckground.setToStartFrame();
      vRevealBa1ckground.setVisibility(View.INVISIBLE);
    }
  }
}
